import React, { PureComponent } from 'react';
import { Tree } from 'antd';
import { connect } from 'dva';
import "./form.less"
const { TreeNode } = Tree;
class StructureTree extends PureComponent {
    constructor(props) {
        super(props);
        this.state = {
       
        }
        this.onSelect=this.onSelect.bind(this)
    }
    renderTreeNodes(data) {
        return data.map(item => {
            const icon=item.selectable?"./static/images/icon2.png":"./static/images/icon1.png"
            if (item.children&&item.children.length) {
                return (
                    <TreeNode icon={<img src={icon} alt="" />}  title={item.title} key={item.key} dataRef={item}>
                        {this.renderTreeNodes(item.children)}
                    </TreeNode>
                );
            }
            return <TreeNode icon={<img src={icon} alt="" />}  title={item.title} key={item.key} {...item} dataRef={item}></TreeNode>

        });
    }
    componentDidMount() {
        if (this.props.onLoaded) {
            this.props.onLoaded(this);
        }
    }
    async onSelect(keys, e) {
        if(this.props.onSelect){
            this.props.onSelect(keys, e)
        }
    }
    render() {
        const { treeData, expandedKeys,selectedKeys } = this.props;
        return (
            <div className="structureTree absMiddle">
                {treeData && treeData.length ? <Tree
                showIcon
                    defaultExpandAll
                    defaultExpandedKeys={expandedKeys}
                    selectedKeys={selectedKeys}
                    onSelect={this.onSelect}
                    treeData={treeData}
                > {treeData ? this.renderTreeNodes(treeData) : null}</Tree>
                    : null}
            </div>
        )
    }
}
export default connect(
    ({ }) => ({}),
    dispatch => ({
        
    })
)(StructureTree);